<template>
  <div class="app-container page-warehouse">
    <FilterForm :data="filterData" @search="getList" labelWidth="86px" />

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['warehouse:warehouseGrouping:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['warehouse:warehouseGrouping:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['warehouse:warehouseGrouping:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['warehouse:warehouseGrouping:export']"
          >导出</el-button
        >
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="warehouseGroupingList"
      ref="multipleTable"
      @row-click="handleRowClick"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="分组编号" align="center" prop="groupingId" />
      <el-table-column label="分组名称" align="center" prop="groupingName" />
      <el-table-column label="分组成员" align="center" prop="groupingMember" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click.native.stop="handleUpdate(scope.row)"
            v-hasPermi="['warehouse:warehouseGrouping:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click.native.stop="handleDelete(scope.row)"
            v-hasPermi="['warehouse:warehouseGrouping:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改木业仓库分组信息对话框-->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="65%"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="分组名称" prop="groupingName">
          <el-input v-model="form.groupingName" placeholder="请输入分组名称" />
        </el-form-item>
        <el-form-item
          class="select-check-group"
          label="分组成员"
          prop="groupingMember"
        >
          <el-select
            v-model="form.groupingMember"
            multiple
            collapse-tags
            filterable
            @change="changeSelect"
            placeholder="请输入分组成员"
            clearable
            size="small"
            popper-class="select-options"
          >
            <el-checkbox v-model="checked" @change="selectAll"
              >全选</el-checkbox
            >
            <el-option
              v-for="(item, index) in warehouseList"
              :key="index"
              :label="item.warehouseName"
              :value="item.warehouseId"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="groupingMember">
          <el-card class="box-card card-item">
            <div slot="header" class="clearfix">
              <span>仓库列表</span>
            </div>
            <template v-for="(item, index) in warehouseList">
              <div v-if="form.groupingMember.includes(Number(item.warehouseId))"  :key="index" class="text item">
                {{ item.warehouseName }}
              </div>
            </template>
          </el-card>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listWarehouseGrouping,
  getWarehouseGrouping,
  delWarehouseGrouping,
  addWarehouseGrouping,
  updateWarehouseGrouping,
  exportWarehouseGrouping,
} from "@/api/business/warehouse/warehouseGrouping";

export default {
  name: "WarehouseGrouping",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 木业仓库分组信息表格数据
      warehouseGroupingList: [],
      // 仓库信息列表
      warehouseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        groupingName: null,
        groupingMember: null,
      },
      // 表单参数
      form: {
        groupingMember: [],
      },
      warehouse: [],
      checked: false,
      // 表单校验
      rules: {
        groupingName: [
          { required: true, message: "请输入分组名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 2 到 20 个字符", trigger: "blur" },
        ],
        groupingMember: [
          { required: true, message: "请输入分组成员", trigger: "blur" },
        ],
      },
      filterData: [
        {
          label: "分组名称",
          name: "groupingName",
          type: "none",
        },
        {
          label: "分组成员",
          name: "groupingMember",
          type: "none",
        },
  
      ],
    };
  },
  created() {
    this.getList();
    this.getWarehouseList();
  },
  methods: {
    /** 查询木业仓库分组信息列表 */
    getList() {
      this.loading = true;
      listWarehouseGrouping(this.queryParams).then((response) => {
        this.warehouseGroupingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询木业仓库信息列表 */
    getWarehouseList() {
      this.$Api.warehouse.list().then((res) => {
        this.warehouseList = res.rows;
      });
    },
    // 穿梭框数据
    getNowWarehouse(val) {
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        groupingName: null,
        groupingMember: [],
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.groupingId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 选中行选中复选框
    handleRowClick(row, column, event) {
      this.$refs.multipleTable.toggleRowSelection(row);
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加木业仓库分组信息";
    },
    // 全选选项
    selectAll() {
      this.form.groupingMember = [];
      if (this.checked) {
        this.warehouseList.map((item) => {
          this.form.groupingMember.push(item.warehouseId);
        });
      } else {
        this.form.groupingMember = [];
      }
    },
    changeSelect(val) {
      if (val.length === this.warehouseList.length) {
        this.checked = true;
      } else {
        this.checked = false;
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const groupingId = row.groupingId || this.ids;
      getWarehouseGrouping(groupingId).then((res) => {
        let { groupingMember } = res.data;
        // groupingMember.split(',')
        res.data.groupingMember = groupingMember.split(",").map((v) => {
          v = Number(v);
          return v;
        });
        this.form = res.data;

        // this.form.groupingMember = groupingMember.split(",").forEach((v) => {
        //   v = Number(v);
        // });

        this.open = true;
        this.title = "修改仓库分组信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        let postData = JSON.parse(JSON.stringify(this.form));
        postData.groupingMember = postData.groupingMember.join();
        // let groupArr = ;
        // this.form.groupingMember = groupArr.toString();
        if (valid) {
          if (this.form.groupingId != null) {
            updateWarehouseGrouping(postData).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWarehouseGrouping(postData).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const groupingIds = row.groupingId || this.ids;
      this.$confirm(
        '是否确认删除仓库分组信息编号为"' + groupingIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delWarehouseGrouping(groupingIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有仓库分组信息数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportWarehouseGrouping(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep.page-warehouse {
  .select-check-group {
    .el-checkbox:last-of-type {
      margin-left: 5px;
    }
  }
  .el-table {
    margin: 0 15px;
    width: calc(100% - 30px);
    border: 1px solid #eaeaea;
    border-radius: 5px;

    .cell {
      font-size: 12px;
      cursor: pointer;
      text-align: center;
    }
    .el-input--medium {
      .el-input__inner {
        height: 24px;
        line-height: 24px;
      }
      .el-input__icon {
        line-height: 24px;
      }
    }
    td {
      padding: 3px;
      & + td {
        border-left: 1px solid #eaeaea;
      }
    }
    .el-table__body {
      td {
        padding: 5px 0;
        .el-button--mini.is-circle {
          padding: 5px;
        }
      }
    }
  }
  .box-card  .el-card__body {
    display: flex ;
    justify-content: space-around;
  }
  .select-options{
    ul{
      .el-checkbox:last-of-type {
        margin-left: 20px;
      }
    }
  }
}
</style>
